import { request } from '@umijs/max';
import { message } from 'antd';
import { getIntl } from 'umi';


export async function createSp(data?: { [key: string]: any }) {
    return request('/admin-api/admin/sp/create', {
        method: 'POST',
        data: data
    });
}
export async function updateSp(data?: { [key: string]: any }) {
    return request('/admin-api/admin/sp/update', {
        method: 'POST',
        data: data
    });
}
export async function activeSp(data?: { [key: string]: any }) {
    return request('/admin-api/admin/sp/active', {
        method: 'POST',
        params: data
    });
}
export async function pagedAPi(
    params: {
        current?: number;
        pageSize?: number;
    },
    options?: { [key: string]: any },
) {
    const intl = getIntl(); 
    try {
        const response = await request<API.ApiResponse>('/admin-api/admin/sp/page', {
            method: 'GET',
            params: {
                ...params,
            },
            ...(options || {}),
        });

        if (response?.code !== 0) {
            message.error(response?.msg  +  intl.formatMessage({ id: 'request.error.default' }) );
            return [];
        }

        return response?.data || [];
    } catch (error) {
        message.error(intl.formatMessage({ id: 'request.error.network' }) );
        return [];
    }
}
export async function queryApiRelations(
    params: {
        current?: number;
        pageSize?: number;
        spId?: string;
    },
    options?: { [key: string]: any },
) {
    const intl = getIntl(); 
    try {
        const response = await request<API.ApiResponse>('/admin-api/admin/sp/queryApiRelations', {
            method: 'GET',
            params: {
                ...params,
            },
            ...(options || {}),
        });

        if (response?.code !== 0) {
            message.error(response?.msg  +  intl.formatMessage({ id: 'request.error.default' }) );
            return [];
        }

        return response?.data || [];
    } catch (error) {
        message.error(intl.formatMessage({ id: 'request.error.network' }) );
        return [];
    }
}
export async function unbindApi(data?: { [key: string]: any }) {
    return request('/admin-api/admin/sp/unbindApi', {
        method: 'POST',
        params: data
    });
}
export async function bindApi(data?: { [key: string]: any }) {
    return request('/admin-api/admin/sp/bindApi', {
        method: 'POST',
        params: data
    });
}
export async function selectListNotInSp(spId: string) {
    return request<API.ApiResponse>('/admin-api/admin/sp/selectListNotInSp', {
        method: 'GET',
        params: {
            spId,
        }
    });
}

export async function getProviderApiTree() {
    return request<API.ApiResponse>('/admin-api/admin/sp/provider_tree', {
        method: 'GET',
    });
}